module.exports = {
  devServer: {
    onBeforeSetupMiddleware: function (devServer) {
      const { app } = devServer;

      //模拟用户列表接口信息+分页
      var userlist = [{
        "id": 15,
        "UserName": "李四",
        "Password": "qwerty",
        "CreateTime": "2023/5/12 星期五 14:30:00",
        "RoleName": "运营主管",
        "Mobile": "13912345678",
        "Email": "lisi@example.com",
        "MgState": true
      }, {
        "id": 14,
        "UserName": "王五",
        "Password": "asdfgh",
        "CreateTime": "2023/4/20 星期四 09:15:00",
        "RoleName": "客服经理",
        "Mobile": "13887654321",
        "Email": "wangwu@company.cn",
        "MgState": false
      }, {
        "id": 13,
        "UserName": "赵六",
        "Password": "zxcvbn",
        "CreateTime": "2023/3/8 星期三 16:45:00",
        "RoleName": "财务专员",
        "Mobile": "13711223344",
        "Email": "zhaoliu@finance.com",
        "MgState": true
      }, {
        "id": 12,
        "UserName": "王七",
        "Password": "123abc",
        "CreateTime": "2023/2/14 星期二 11:20:00",
        "RoleName": "技术总监",
        "Mobile": "13644556677",
        "Email": "qianqi@tech.org",
        "MgState": false
      }, {
        "id": 11,
        "UserName": "孙八",
        "Password": "abc456",
        "CreateTime": "2023/1/30 星期一 13:10:00",
        "RoleName": "产品经理",
        "Mobile": "13577889900",
        "Email": "sunba@product.com",
        "MgState": true
      }, {
        "id": 10,
        "UserName": "周九",
        "Password": "789xyz",
        "CreateTime": "2022/12/25 星期日 10:05:00",
        "RoleName": "UI设计师",
        "Mobile": "13466778899",
        "Email": "zhoujiu@design.cn",
        "MgState": false
      }, {
        "id": 9,
        "UserName": "王十",
        "Password": "xyz123",
        "CreateTime": "2022/11/11 星期五 15:35:00",
        "RoleName": "测试工程师",
        "Mobile": "13355667788",
        "Email": "wushi@qa.com",
        "MgState": false
      }, {
        "id": 8,
        "UserName": "郑十一",
        "Password": "pass11",
        "CreateTime": "2022/10/1 星期六 08:00:00",
        "RoleName": "运维主管",
        "Mobile": "13244556677",
        "Email": "zheng11@ops.net",
        "MgState": true
      }, {
        "id": 7,
        "UserName": "王十二",
        "Password": "word12",
        "CreateTime": "2022/9/9 星期五 17:55:00",
        "RoleName": "市场专员",
        "Mobile": "13133445566",
        "Email": "wang12@market.com",
        "MgState": false
      }, {
        "id": 6,
        "UserName": "陈十三",
        "Password": "key13",
        "CreateTime": "2022/8/8 星期一 12:25:00",
        "RoleName": "销售经理",
        "Mobile": "13022334455",
        "Email": "chen13@sales.cn",
        "MgState": false
      }]

      //添加用户信息的接口
      app.get('/api/adduser', (req, res) => {
        const { username, password, email, mobile, query, pagenum, pagesize } = req.query   //拿到全部用户的信息
        var addUser = {  //添加的那个人
          "id": userlist[0].id + 1,   //最后一个人的id+1
          "UserName": username,
          "Password": password,
          "CreateTime": "2025/5/20 星期二 14:30:00",
          "RoleName": "超级管理员",
          "Mobile": mobile,
          "Email": email,
          "MgState": true   //true
        }
        userlist.push(addUser)  //将新人添加到用户列表
        userlist.sort(function (a, b) {
          return b.id - a.id   //降序排列
        })
        total = userlist.length //更新用户列表的总长度
        if (pagenum == 0) {  //分页查询公式
          newdatalist = userlist.slice(pagenum * pagesize, (parseInt(pagenum) + 1) * pagesize)
        } else {
          newdatalist = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
        }
        //向前端返回数据
        res.json({
          'total':total,  //向前端返回新的总长度
          'data':newdatalist  //向前端返回新的用户列表
        })
      })

      //修改用户状态的接口
      app.get('/api/userupdate', (req, res) => {
        const { id, MgState } = req.query
        var count = 0
        var newState = ''
        if (userlist) {
          var user = userlist.filter(u => u.id == id)
          count = user.length
          user.MgState = MgState
          newState = user.MgState
        }
        res.json({
          'count': count,
          'newState': newState
        })
      })


      app.get('/api/users', (req, res) => {
        const { query, pagenum, pagesize } = req.query //查询关键字query\页码pagenum\每页的条数pagesize
        var total = 0
        //注意：(parseInt(pagenum) + 1) 两个数相加时，
        //一定要通过parseInt(pagenum)把传过来的参数转换成int类型
        //不然如pagenum为1 ，会出现这样错误：pagenum + 1=11。
        var newdatalist = []
        if (query != "") {  //有查询关键词
          total = userlist.filter(u => u.UserName.includes(query)).length  //模糊查询
          if (pagenum == 0) {
            newdatalist = userlist.filter(u => u.UserName.includes(query))
              .slice(pagenum * pagesize, (parseInt(pagenum) + 1) * pagesize)
          } else {
            newdatalist = userlist.filter(u => u.UserName.includes(query))
              .slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
          }

        } else {  //没有查询关键词
          total = userlist.length  //全部长度
          if (pagenum == 0) {
            newdatalist = userlist.slice(pagenum * pagesize, (parseInt(pagenum) + 1) * pagesize)
          } else {
            newdatalist = userlist.slice((parseInt(pagenum) - 1) * pagesize, pagenum * pagesize)
          }
        }



        res.json({
          'status': 'ok',
          'total': total,
          'data': newdatalist
        })
      })


      //模拟左侧菜单接口
      app.get('/api/menu', (req, res) => {
        res.json({
          "status": "ok",
          "data": [{
            "id": 1,
            "name": "用户管理",
            "child": [{
              "id": 1,
              "name": "用户列表",
              "path": "/users"
            }]
          }, {
            "id": 2,
            "name": "权限管理",
            "child": [{
              "id": 2,
              "name": "角色列表",
              "path": "/rote"
            }, {
              "id": 3,
              "name": "权限列表",
              "path": "/right"
            }]
          }, {
            "id": 3,
            "name": "商品管理",
            "child": [{
              "id": 4,
              "name": "商品列表",
              "path": "/product"
            }, {
              "id": 5,
              "name": "分类参数",
              "path": "/userss"
            }, {
              "id": 6,
              "name": "商品分类",
              "path": "/producttype"
            }]
          }, {
            "id": 4,
            "name": "订单管理",
            "child": []
          }, {
            "id": 5,
            "name": "数据统计",
            "child": []
          }]
        })
      })

      //模拟登陆的一个接口
      let tockenStr = 'dhclass'
      //(req,res)回调方法，req：请求；res:返回的结果
      app.get('/api/login', (req, res) => {
        const { username, password } = req.query
        if (username == 'admin' && password == '123456' ||
          username == 'dh' && password == '123456') {
          res.json({
            code: 0,
            message: '登陆成功',
            tocken: tockenStr + '-' +
              username + '-' +
              (new Date().getTime() + 60 * 60 * 1000)
          })
        } else {
          res.json({
            code: 1,
            message: '账号或密码错误'
          })
        }
      })
    }
  }
};